package com.coocaa.ops.admin.v1.audit.service;

import com.coocaa.ops.admin.v1.audit.model.ExecutionTaskModel;
import com.coocaa.ops.admin.v1.audit.provider.IAuditTaskProvider;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import lombok.Setter;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.Set;

/**
 * @author bijiahao
 * @date : 2018/7/10.
 * @description
 */
public class AuditTaskProviderManager {
    /**
     * 各个资源的处理提供者实例 Map集合
     */
    @Setter
    private Map<String, IAuditTaskProvider> auditTaskProviderMap;

    /**
     * 验证是否存在对应的处理方法
     *
     * @param resourceType
     * @return
     */
    public boolean hasProvider(String resourceType) {
        Set<String> keys = auditTaskProviderMap.keySet();
        Assert.notNull(keys, "IAuditTaskProvider was not successful!");
        return keys.contains(resourceType);
    }

    /**
     * 上架操作
     *
     * @param id
     * @param version
     * @param userId
     * @param type
     */
    public void putShelves(Integer id, Integer version, Integer userId, String type) {
        IAuditTaskProvider auditTaskProvider = getAuditTaskProvider(type);
        //创建资源信息实例
        ResourceBaseModel resourceBaseModel = ResourceBaseModel.getResourceBaseModel(type, id, version);
        auditTaskProvider.putShelves(resourceBaseModel, userId);
    }

    /**
     * 审核操作
     *
     * @param id
     * @param version
     * @param userId
     * @param type
     * @param auditStatus
     */
    public void audit(Integer id, Integer version, Integer userId, String type, Integer auditStatus) {
        IAuditTaskProvider auditTaskProvider = getAuditTaskProvider(type);
        //创建资源信息实例
        ExecutionTaskModel executionTaskModel = ExecutionTaskModel.getExecutionTaskModel(id, version, type, auditStatus);
        auditTaskProvider.auditTask(executionTaskModel, userId);
    }

    /**
     * 撤销审核
     *
     * @param resourceBaseModel
     * @param userId
     */
    public void revokedAudit(ResourceBaseModel resourceBaseModel, Integer userId) {
        IAuditTaskProvider auditTaskProvider = getAuditTaskProvider(resourceBaseModel.getType());
        auditTaskProvider.revokedAudit(resourceBaseModel, userId);
    }

    /**
     * 取消定时任务后的操作
     *
     * @param id
     * @param version
     * @param userId
     * @param type
     */
    public void cancelTask(Integer id, Integer version, Integer userId, String type) {
        IAuditTaskProvider auditTaskProvider = getAuditTaskProvider(type);
        //创建资源信息实例
        ResourceBaseModel resourceBaseModel = ResourceBaseModel.getResourceBaseModel(type, id, version);
        auditTaskProvider.cancelTask(resourceBaseModel, userId);
    }

    /**
     * 获取服务提供者实例
     *
     * @param type
     * @return
     */
    public IAuditTaskProvider getAuditTaskProvider(String type) {
        IAuditTaskProvider auditTaskProvider = auditTaskProviderMap.get(type);
        Assert.notNull(auditTaskProvider, "The corresponding IAuditTaskProvider  instance object cannot be empty！type = "+ type);
        return auditTaskProvider;
    }
}
